//
//  ViewController.m
//  UI14 - UICollectionVIew
//
//  Created by 韩旭 on 16/8/2.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"

#define WIDETH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define COLORAUTO [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0]

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
#define kCollectionView @"reuse"
#define kCollectionReusable @"ReuseView"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collection;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor cyanColor];
    _collection.delegate = self;
    _collection.dataSource = self;
    NSLog(@"%@", NSStringFromCGSize(layout.itemSize));
    //    区头大小 宽横向滚动时设置宽 纵向滚动时, 设置高
    layout.headerReferenceSize = CGSizeMake(0, 100);
    //    区尾大小 宽横向滚动时设置宽 纵向滚动时, 设置高
    layout.footerReferenceSize = CGSizeMake(100, 100);
    //   1. itemSize 设置大小
    //    itemSize默认大小时（50，50）
    //   2. 设置滚动方向：
    //    UICollectionViewScrollDirectionVertical 水平滚动
    //    UICollectionViewScrollDirectionHorizontal 竖直滚动
    //    滚动方向改变会交换列和行
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //   3. 最小行间距  minimumLineSpacing
    layout.minimumLineSpacing = 5;
    //   4. 最小列间距  minimumInteritemSpacing
    layout.minimumInteritemSpacing = 5;
    //   5. sectionInset
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    layout.itemSize = CGSizeMake(200, 120);
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 7.5, 120);
    //    创建 UICollectionView
    //    参数1. frame
    //    参数2. 填入不能为空的layout（布局）
    [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionView];
    [_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionReusable];
    [_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionReusable];
    //    8. IOS9.0 之后推出的 区头悬浮效果
    layout.sectionHeadersPinToVisibleBounds = YES;
    //    8. IOS9.0  区尾悬浮效果
    layout.sectionFootersPinToVisibleBounds = YES;
    [self.view addSubview:_collection];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionReusable forIndexPath:indexPath];
        reuseableView.backgroundColor = [UIColor redColor];
        return reuseableView;
    } else {
        UICollectionReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionReusable forIndexPath:indexPath];
        reuseableView.backgroundColor = [UIColor blueColor];
        return reuseableView;
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionView forIndexPath:indexPath];
    cell.contentView.backgroundColor = COLORAUTO;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
